'''
教程原地址：
https://tensorflow.google.cn/tutorials/keras/classification?hl=zh-cn
'''

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# 第一步：加载数据集
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

train_images = train_images / 255.0
test_images = test_images / 255.0


# 第二步：构建模型
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])

# 第三步：配置模型
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

# 第四步：训练模型------到这一步结束，模型已经训练好了
model.fit(train_images, train_labels, epochs=10)

# *第五步：保存模型*
model.save('clothing_model.keras')

# --------------------到此为止，模型训练结束，下面是预测和评估模型的步骤-------------------------------

# 第五步：评估模型
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)

print('\nTest accuracy:', test_acc)

# 第六步：预测模型
# 把模型封装起来，在外城增加了softmax层
probability_model = tf.keras.Sequential([model,
                                         tf.keras.layers.Softmax()])

predictions = probability_model.predict(test_images)  # 预测概率
